const app = getApp();
import { getOpenID } from '../../../../common/utils/ws/wsxcx-opendata-sdk';
Page({
  data: {
    formlist: [],
    shareInfo: {
      alias:'',
      img_url: '',
      title: '',
    },
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
  },

  toDetail(e) {
    const { id = '' } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../index/index?id=${id}`,
    });
  },

  collectFormId(e) {
    getOpenID().then(openid => {
      app.util.request({
        url: 'pushService/api/formid',
        module: 'cm_universal_form',
        data: {
          formid: e.detail.formId,
          openid,
        },
        method: 'GET',
        success: res => {
          console.log('=====collectFormId success', res);
        },
      });
    });
  },

  getList() {
    app.util.request({
      url: 'entry/wxapp/api',
      module: 'cm_universal_form',
      data: {
        type: 6,
      },
      method: 'GET',
      success: res => {
        res.data && res.data.data && this.setData({
          formlist: res.data.data,
        });
      },
    });
  },

  getShareInfo() {
    app.util.request({
      url: 'entry/wxapp/api',
      module: 'cm_universal_form',
      data: {
        type: 5,
      },
      method: 'GET',
      success: res => {
        console.log('=====getShareInfo success', res);
        res.data&&res.data.data&&this.setData({
          shareInfo: res.data.data,
        });
      },
    });
  },

  toHome() {
    const isCompanyCloudTemplate = Number(app.siteInfo.isCloudTemplate) === 1;
    if (isCompanyCloudTemplate) {  // 企业云端发布
      app.util.navigateTo('ws_micro_company_collection/pages/index/index', '', true);
      return;
      }
    let appJson = app.siteInfo.appJson[app.tabbarindex || 0];
    const url = appJson.homePage || (appJson.tabBar && appJson.tabBar.list && appJson.tabBar.list[0].pagePath);
    app.util.navigateTo(url, {}, 'all');
  },

  goHome() {
    const isCompanyCloudTemplate = Number(app.siteInfo.isCloudTemplate) === 1;
    if (isCompanyCloudTemplate) {  // 企业云端发布
      app.util.navigateTo('ws_micro_company_collection/pages/index/index', '', true);
      return;
    }
    let appJson = app.siteInfo.appJson[app.tabbarindex || 0];
    const url = appJson.homePage || (appJson.tabBar && appJson.tabBar.list && appJson.tabBar.list[0].pagePath);
    app.util.navigateTo(url, {}, 'all');
  },

  onShareAppMessage() {
    const { shareInfo : { img_url: imageUrl ='', title = '' } } = this.data;
    return {
      title,
      imageUrl,
    };
  },

  onLoad() {
    this.getList();
    this.getShareInfo();
  },
});
